package com.wangxiaonan.berry.controller;

import com.alibaba.fastjson.JSONObject;
import com.wangxiaonan.berry.entity.TongDun;
import com.wangxiaonan.berry.handler.ResponseWrapper;
import com.wangxiaonan.berry.service.TongDunService;
import javax.annotation.Resource;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

/**
 * 同盾 https://www.tongdun.cn
 *
 * @Author: wangxiaonan
 * @Date: 2019/7/2
 **/
@CrossOrigin()
@RestController
@RequestMapping("/tongdun")
public class TongDunController {

    private static final Logger logger = LoggerFactory.getLogger(TongDunController.class);

    @Resource
    private TongDunService tongDunService;

    @RequestMapping(value = "/riskService/v1.1", method = RequestMethod.POST)
    public ResponseWrapper riskService(@RequestBody TongDun tongDun) {
        ResponseWrapper wrapper = new ResponseWrapper();
        String data = tongDunService.riskService(tongDun);
        JSONObject result = JSONObject.parseObject(data);
        logger.info("同盾返回参数：{}", result);
        String success = result.getString("success");
        if ("true".equals(success)) {
            JSONObject deviceInfo = result.getJSONObject("device_info");
            wrapper.success().setData(deviceInfo);
        } else {
            String reasonCode = result.getString("reason_code");
            wrapper.fail(success).setMessage(reasonCode);
        }
        return wrapper;
    }
}
